<template>
  <div>
    <search-table
      :url="url"
      ref="searchtable"
      :columns="columns"
      :defaultParams="defaultParams"
      :headParams="headParams"
      :form="form"
      :xLength="1400"
    >
      <!-- 搜索条件 -->
      <template slot="searchForm">
        <a-form layout="inline" :form="form">
          <a-form-item label="活动名称">
            <a-input
              allowClear
              v-decorator="['name']"
              placeholder="请输入活动名称"
            ></a-input>
            <a-input type="hidden" v-decorator="['type']"></a-input>
          </a-form-item>
          <a-form-item label="状态">
            <a-select
              showSearch
              allowClear
              style="width: 150px"
              :options="statusList"
              v-decorator="['status']"
              placeholder="请选择"
            >
            </a-select>
          </a-form-item>
          <!-- <a-form-item label="是否参与活动">
            <a-select
              allowClear
              style="width: 150px"
              :options="yesOrNo"
              v-decorator="['isIn']"
              placeholder="请选择">
            </a-select>
          </a-form-item> -->
        </a-form>
      </template>
      <!-- 表格行操作 -->
      <template slot="tableOperation" slot-scope="{ record }">
        <a-space>
          <span class="text-primary pointer" @click="copy(record)">复制</span>
          <permission v-if="record.status != 1" authority="group:activity:info">
            <span class="text-primary pointer" @click="openInfo(record)"
              >查看</span
            ><span>(只查看商品详情不可编辑)</span>
          </permission>
          <permission
            v-if="record.status == 1"
            authority="group:activity:modify"
          >
            <span class="text-primary pointer" @click="editInfo(record.id)"
              >查看</span
            ><span>(查看商品详情可编辑)</span>
          </permission>
          <permission
            v-if="record.status != 2"
            authority="group:activity:delete"
          >
            <a-popconfirm
              title="是否确认删除？"
              :getPopupContainer="getPopupContainer()"
              @confirm="() => deleteRows(record.id)"
            >
              <a href="javascript:;" class="text-danger pointer"
                >删除(可删除)</a
              >
            </a-popconfirm>
          </permission>
          <permission
            v-if="record.status == 2"
            authority="group:activity:delete"
          >
            <a href="javascript:;" class="text-notallowed">删除(不可删除)</a>
          </permission>
        </a-space>
      </template>
    </search-table>
  </div>
</template>

<script>
import { findDictNameByValue, createOptionsByDict } from "@/utils/dict";
export default {
  name: "MarketingGroupBargainingTable",
  props: {
    // 自身的tab
    tab: {
      type: String,
      required: true,
      default: "",
    },
    // 当前活跃的tab
    curTab: {
      type: String,
      required: true,
      default: "",
    },
  },
  data() {
    return {
      statusList: createOptionsByDict("MARKETING_STATUS"), // 营销活动状态
      yesOrNo: createOptionsByDict("YES_NO"), // 是否
      url: "/group/activity/page", // 列表接口地址
      headParams: { baseURL: `${location.origin}/goldenhome/ghgroup/admin` }, // 接口域名
      deleteUrl: "/group/activity/delete", // 删除接口
      currentRecord: {}, // 选中行数据
      form: this.$form.createForm(this, { name: "table_search" }), // 搜索表单
      defaultParams: {
        // 搜索默认参数
        type: this.tab,
      },
      columns: [
        {
          title: "活动名称",
          dataIndex: "name",
          key: "name",
          width: 100,
        },
        {
          title: "创建人",
          dataIndex: "createByName",
          key: "createByName",
          width: 100,
        },
        {
          title: "有效时间",
          dataIndex: "startTime",
          key: "startTime",
          width: 200,
          customRender: (text, record) => {
            return record.startTime + "至" + record.endTime;
          },
        },
        {
          title: "状态",
          dataIndex: "status",
          key: "status",
          width: 100,
          customRender: (text, record) =>
            findDictNameByValue("MARKETING_STATUS", record.status),
        },
        // {
        //   title: "是否参与活动",
        //   dataIndex: "isCy",
        //   key: "isCy",
        //   customRender: (text, record) => findDictNameByValue('YES_NO', record.type)
        // },
        {
          title: "已成团订单数",
          dataIndex: "successOrderNum",
          key: "successOrderNum",
          align: "center",
        },
        {
          title: "实际成团人数",
          dataIndex: "successMemberNum",
          align: "center",
          key: "successMemberNum",
        },
        {
          title: "销售金额（元）",
          dataIndex: "successOrderPrice",
          align: "center",
          key: "successOrderPrice",
          customRender: (text) => {
            return (Number(text) / 100).toFixed(2);
          },
        },
        {
          title: "操作",
          dataIndex: "operation",
          width: 350,
          fixed: "right",
          scopedSlots: { customRender: "operation" },
        },
      ],
    };
  },
  watch: {
    curTab(val) {
      this.checkCurTab();
    },
  },
  created() {},
  mounted() {},
  methods: {
    // 删除二次确认
    getPopupContainer() {
      return (triggerNode) => triggerNode.parentNode.parentNode;
    },
    // 刷新当前tab
    checkCurTab() {
      if (this.curTab == this.tab) {
        this.$nextTick(() => {
          this.$refs.searchtable.refreshTable();
        });
      }
    },
    // 打开拼团信息
    openInfo(record) {
      this.currentRecord = record;
      this.$router.push({
        name: "MarketingGroupBargainingPagesForm",
        query: {
          handle: "view",
          id: record.id,
          type: this.curTab,
        },
      });
    },
    copy(record) {
      this.currentRecord = record;
      this.$router.push({
        name: "MarketingGroupBargainingPagesForm",
        query: {
          handle: "copy",
          id: record.id,
          type: this.curTab,
        },
      });
    },
    // 编辑拼团信息
    editInfo(id) {
      this.$router.push({
        name: "MarketingGroupBargainingPagesForm",
        query: {
          handle: "edit",
          id: id,
          type: this.curTab,
        },
      });
    },
    // 删除拼团活动
    deleteRows(id) {
      this.$post(
        this.deleteUrl,
        { id },
        { baseURL: `${location.origin}/goldenhome/ghgroup/admin` }
      ).then((res) => {
        if (res && res.returnCode === 1000) {
          this.$message.success("删除成功");
          this.$refs.searchtable.refreshTable();
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.text-notallowed {
  color: #b0b0b0;
  cursor: not-allowed;
}
</style>
